/*
 * @Author: GaoTang
 * @Company: ShineSoft
 * @Date: 2021-07-19 19:49:01
 * @LastEditors: GaoTang
 * @LastEditTime: 2021-07-19 23:33:27
 * @FilePath: \tpl-uni-https\api\base.js
 * @Email: qq23745038@126.com
 */
import axios from "./config";
import store from "@/store";
import { msg } from "@/common/js/util";

export const post = (url, data, extend = { cache: false }) => {
  let defaultConfig = {
    url,
    method: "POST",
    data: data,
  };
  let config = { ...defaultConfig, ...extend };
  const uniIdToken = uni.getStorageSync("uniIdToken");
  if (uniIdToken) {
    config.headers = {
      Authorization: `Bearer ${uniIdToken}`,
    };
  }
  if (config.showLoading) {
    uni.showLoading({
      title: "加载中...",
      mask: true,
    });
  }
  return axios(config).then(
    (res) => {
      if (config.showLoading) uni.hideLoading();
      const code = res.code;
      if (code === 1001) {
        msg("登录信息已过期，请重新登录");
        store.commit("logout");
        Promise.reject("无效的登录信息");
        return;
      }
      return res;
    },
    (err) => {
      if (config.showLoading) uni.hideLoading();
      return Promise.reject(err);
    }
  );
};

export const get = (url, data, extend = { cache: false }) => {
  let defaultConfig = {
    url,
    method: "GET",
    params: data,
  };
  let config = { ...defaultConfig, ...extend };

  const uniIdToken = uni.getStorageSync("uniIdToken");
  if (uniIdToken) {
    config.headers = {
      Authorization: `Bearer ${uniIdToken}`,
    };
  }
  if (config.showLoading) {
    uni.showLoading({
      title: "加载中...",
      mask: true,
    });
  }
  return axios(config).then(
    (res) => {
      if (config.showLoading) uni.hideLoading();
      return res;
    },
    (err) => {
      if (config.showLoading) uni.hideLoading();
      return Promise.reject(err);
    }
  );
};
